/****************************************************************************
**
** Copyright (C) 2009-2010 Mist Poryvaev.
** All rights reserved.
** Contact: Mist Poryvaev (mist.poryvaev@gmail.com)
**
** This file is part of the RailWorm project.
**
****************************************************************************/

#ifndef PARSETRAINDATA_H
#define PARSETRAINDATA_H

#include <QtCore>
#include <QtGui>
#include <QtNetwork>
#include "htmlgenie.h"

struct FirmTrainStruct {
	QString Number1, Number2;
	QString Name;
};

typedef QList<FirmTrainStruct> FirmTrainsList;

struct CarDataStruct {
	QString VehicleCode;

	QString Number;
	QString UpperPlaces;
	QString LowerPlaces;
	QString UpperSidePlaces;
	QString LowerSidePlaces;
	QString OtherPlaces;
};

typedef QList<CarDataStruct> CarDataList;

struct AdvClassDesc {
	QString Class;
	QString Service;
	QString Category;
};

struct ClassesStruct {
	QString Class;
	QString Category;
	QString Price;
	AdvClassDesc AdvDesc;
	CarDataList Car;

	int Id;

	bool Visible;
};

typedef QList<ClassesStruct> ClassesList;

struct TrainDataStruct {
// ------ mainpage
	QString OriginalName, Name, NameUrl;
	int Id;
	QString Path, PathUrl;
	QString From, To;
	QString Start;
	QString End;
	QString Delta;
// ----- subpage
	ClassesList Class;
// ----- visible
	bool Visible;
// ----- favorite
	bool Favorite;
};

typedef QList<TrainDataStruct> TrainDataList;

class ParseTrainData;
class TableEventHandler: public QObject {
	Q_OBJECT
public:
	TableEventHandler(ParseTrainData * PTD);
	void Reset();
protected:
	bool eventFilter(QObject * obj, QEvent * ev);
private:
	ParseTrainData * m_PTD;
	QTableWidgetItem * m_PrevItem;
	QPushButton * m_PathButton;
	QPushButton * m_NameButton;
	QPushButton * m_OrderButton;
private slots:
	void NameButtonClicked();
	void PathButtonClicked();
	void OrderButtonClicked();
};

class ParseTrainData: public QObject {
	Q_OBJECT
	friend class TableEventHandler;
public:
	ParseTrainData(QObject * Parent = 0);
	~ParseTrainData();
	void SetFirmTrainsList(FirmTrainsList * FirmTrains);
	QTableWidget * Table();
	QWidget * Widget();
	QLineEdit * Filter();
	const TrainDataList & SessionData();
	void SetCollapsable(bool State);
	void SetSeatsDetailsVisible(bool State);
	void SortBy(int Col);
	enum {
		COL_NAME = 0,
		COL_PATH,
		COL_START,
		COL_END,
		COL_DELTA,
		COL_CLASS,
		COL_CATEGORY,
		COL_PRICE,
		COL_NUMBER,
		COL_TOTALPLACES,
		COL_UPPERPLACES,
		COL_LOWERPLACES,
		COL_UPPERSIDEPLACES,
		COL_LOWERSIDEPLACES,
		COL_OTHERPLACES,
		COLS_COUNT};
	enum {
		ROLE_TRAIN_ID = Qt::UserRole,
		ROLE_CLASS_ID,
		ROLE_FAVORITE
	};
	enum {
		TABLE_ALL = 0,
		TABLE_FAV
	};
public slots:
	void Exec(const QString & From, const QString & To, const QString & DateStart, const QString & DateEnd);
	void Print();
	void Cancel();
private:
	HtmlGenie jinn;
	int m_Class_Id;
	QString m_From, m_To;
//	QVector<int> m_MainPagesId, m_SubPagesId;
	int m_MainPagesCount, m_MainPagesFinished;
	int m_SubPagesCount, m_SubPagesFinished;
	TrainDataList m_SessionData, m_TempSessionData, m_FavData;
	TrainDataList * m_CurrentData;
	QTableWidget * m_Table;
	QLineEdit * m_Filter;
	QComboBox * m_TableType;
	QWidget * m_Widget;
	FirmTrainsList * m_FirmTrains;
	TableEventHandler * m_TableEventHandler;
	bool m_Collapsable;
	bool m_SeatsDetailsVisible;
	void ParseMainPage(HtmlResponse * Response);
	void ParseSubPage(HtmlResponse *, int Id);
	void CreateAdvClassDesc(ClassesStruct & Class);
	void CreateTableHeaders();
	bool Contains(const TrainDataStruct & TrainData, const QString & Text);
	bool Contains(const ClassesStruct & ClassData, const QString & Text, bool Exact = false);
	QString ExtractUrl(int Row, int Column);
	TrainDataList::iterator FindById(const TrainDataList & List, int Id);
	ClassesList::iterator FindClassById(const TrainDataList::iterator & _it, int _Class_Id);
private slots:
	void OnMainPageRequestFinished(HtmlResponse * Response, int Id, bool _Error);
	void OnSubPageRequestFinished(HtmlResponse * Response, int Id, bool _Error);
	void OnSort(int Index);
	void OnFilterChanged();
	void OpenUrl(int Row, int Column);
	void MarkFavorite(int Row, int Column);
	void TableTypeChanged(int Type);
	void Order(int Row, int Column);
signals:
	void MainPageFinished();
	void SubPagesFinished();
	void SubPagesProcessing();
	void Error(QString Text);
	void ProgressUpdate(qint64 Done, qint64 Total);
	void OrderRequested(TrainDataStruct);
};

#endif // PARSETRAINDATA_H
